<?php

/**
 * @author ducnhat
 * @email ddnhat@gmail.com
 * @copyright 2014
 */

class Default_Model_Account extends Zend_Db_Table{
	
	protected $_name    = "accounts";
    protected $_primary = "id";
    
    public function saveData($data){
    	$db = Zend_Registry::get('db');
        
        $db->beginTransaction();
        
        try{
            if(isset($data['id']) && $this->checkExists($data['id'])){
                $id = $data['id'];
                
                $db->update("accounts", $data, "id = $id");
            }else{
                $db->insert('accounts', $data);
            }
            
            $db->commit();
            
            return true;
        }catch(Exception $e){
            $db->rollBack();
            
            return false;
        }
    }
    
    public function getData($id){
    	if($this->checkExists($id)){
            $db = Zend_Registry::get('db');
            $db->setFetchMode(Zend_Db::FETCH_ASSOC);
            
            $select = $db->select()
                        ->from(array('a' => 'accounts'), array('id', 'name', 'password', 'email', 'created', 'status', 'activated', 'point'))
                        ->where('a.id = ?', $id, 'INTEGER');
            
            $rs = $db->fetchRow($select);
            
            return $rs;
        }
        
        return false;
    }
    
    public function getDataByEmail($email){
    	if($this->checkEmailExists($email)){
            $db = Zend_Registry::get('db');
            $db->setFetchMode(Zend_Db::FETCH_ASSOC);
            
            $select = $db->select()
                        ->from(array('a' => 'accounts'), array('id', 'name'))
                        ->where('a.email = ?', $email);
            
            $rs = $db->fetchRow($select);
            
            return $rs;
        }
        
        return false;
    }
    
    public function active($email, $activationCode){
        $result['is_error'] = false;
        $result['message_error'] = "";
        
    	if($this->checkEmailExists($email)){
            $db = Zend_Registry::get('db');
            $db->setFetchMode(Zend_Db::FETCH_ASSOC);
            
            $date = new Zend_Date();
            $curDate = $date->get(Zend_Registry::get('date_format_insert'));
            
            $select = $db->select()
                        ->from(array('a' => 'accounts'), array('id', 'name', 'email', 'created', 'activated'))
                        ->where('a.email = ?', $email);
                        
            $rs = $db->fetchRow($select);
            
            if(!$rs['activated']){
                $now = new DateTime($curDate);
                $created = new DateTime($rs['created']);
                
                $diff = $now->diff($created);
                
                if($diff->d <= 2){
                    $active = new Zenddn_Account_ActivationCode();
                    
                    $name = $rs['name'];
                    $activeCode = $active->generateActivationCode($email, $name);
                    
                    if(strtolower($activationCode) == strtolower($activeCode)){
                        $rs['activated'] = true;
                        $rs['status'] = true;
                        
                        $this->saveData($rs);
                        
                        $result['message_error'] = "active success";
                    }else{
                        $result['is_error'] = true;
                        $result['message_error'] = "activation code invalid";
                    }
                }else{
            	    $result['is_error'] = true;
                    $result['message_error'] = "over 48h";
            	}
            }else{
        	    $result['is_error'] = true;
                $result['message_error'] = "already actived";
        	}
    	}else{
    	    $result['is_error'] = true;
            $result['message_error'] = "email not exists";
    	}
        
        return $result;
    }
    
    public function checkExists($id){
        $option = array('table' => 'accounts', 'field' => 'id');
    	$validator = new Zend_Validate_Db_RecordExists($option);
        
        if($validator->isValid($id)){
            return true;
        }
        
        return false;
    }
    
    public function checkEmailExists($email){
        $option = array('table' => 'accounts', 'field' => 'email');
    	$validator = new Zend_Validate_Db_RecordExists($option);
        
        if($validator->isValid($email)){
            return true;
        }
        
        return false;
    }
}